package com.vf.admin.client.controller;

import com.vf.admin.client.common.ClientBaseController;
import com.vf.admin.client.common.ClientConstants;
import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.client.service.AppRoomService;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.miaodong.entity.AppRoom;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.utils.StringEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author anxiang
 * @version 1.0.0
 * @ClassName AppRoomKaiGuanController.java
 * @Description 房间设置管理
 * @createTime 2021年11月02日 16:02:00
 */
@Controller
@RequestMapping("/app/roomkauiguan")
public class AppRoomKaiGuanController extends ClientBaseController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    AppUserService appUserService;
    @Autowired
    AppRoomService appRoomService;
    @Autowired
    RedisUtil redisUtil;

    /**
     * @Description 礼物特效开关
     * @Param: [leixing] 礼物特效  1开启  2关闭
     * @return: com.vf.admin.common.entity.Result
     * @Author: ljx
     * @Date: 2021/07/2
     */
    @RequestMapping("/liwu")
    @ResponseBody
    public Result liwu(Integer leixing, Integer roomid) {
        try {

            if (StringEx.isNull(leixing)) {
                return this.errorJSON("礼物特效操作类型不存在");
            }

            if (StringEx.isNull(roomid)) {
                return this.errorJSON("房间ID不存在");
            }

            //获取最新数据
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);
            //获取房间
            AppRoom appRoom = appRoomService.getById(roomid);
            if (appRoom == null) {
                return this.errorJSON("房间ID不存在");
            }
            //如果不是房主不能操作
            if (appRoom.getUserid().intValue() != userId.intValue()) {
                return this.errorJSON("没有操作权限");
            }

            // 礼物特效  1开启  2关闭 默认：1正常
            if (appRoom.getGifteffects().intValue() == leixing) {
                return this.sucessJSON("设置成功");
            }
            //开启
            if (leixing.intValue() == 1) {
                appRoomService.updateliwutexiao(roomid, leixing);
            } else if (leixing.intValue() == 2) {//关闭
                appRoomService.updateliwutexiao(roomid, leixing);
            } else {
                return this.errorJSON("操作类型不存在");
            }
            return this.sucessJSON("设置成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug("房间礼物开关接口异常" + e);
            return this.errorJSON("系统繁忙,请联系管理员");
        }
    }

    /**
     * @Description 进房提示开关
     * @Param: [leixing] 进房提示  1开启  2关闭
     * @return: com.vf.admin.common.entity.Result
     * @Author: ljx
     * @Date: 2021/07/2
     */

    @RequestMapping("/tishi")
    @ResponseBody
    public Result tishi(Integer leixing, Integer roomid) {
        try {

            if (StringEx.isNull(leixing)) {
                return this.errorJSON("礼物特效操作类型不存在");
            }

            if (StringEx.isNull(roomid)) {
                return this.errorJSON("房间ID不存在");
            }

            //获取最新数据
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);
            //获取房间
            AppRoom appRoom = appRoomService.getById(roomid);
            if (appRoom == null) {
                return this.errorJSON("房间ID不存在");
            }
            //如果不是房主不能操作
            if (appRoom.getUserid().intValue() != userId.intValue()) {
                return this.errorJSON("没有操作权限");
            }
            // 进房提示  1开启  2关闭 默认：1正常
           /* if (appRoom.getGifteffects().intValue() == leixing) {
                return this.sucessJSON("设置成功");
            }*/
            //开启
            if (leixing.intValue() == 1) {
                appRoomService.updatejinfangtishi(roomid, leixing);
            } else if (leixing.intValue() == 2) {//关闭
                appRoomService.updatejinfangtishi(roomid, leixing);
            } else {
                return this.errorJSON("操作类型不存在");
            }
            return this.sucessJSON("设置成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug("进房提示开关接口异常" + e);
            return this.errorJSON("系统繁忙,请联系管理员");
        }
    }

    @RequestMapping("/gonggao")
    @ResponseBody
    public Result gonggao(Integer roomid, String gonggao) {
        appRoomService.updateroomgonggao(roomid, gonggao);
        return this.sucessJSON("设置成功");
    }
}